<?php
namespace Home\Controller;
use Think\Controller;

class BugController extends Controller{
	protected $mysql;
	
	function __construct(){
		parent::__construct();
		header("Content-type:text/html; charset=utf-8");
		
		$this->mysql	= new \Think\Model();
	}
	
	/*获取Bug列表*/
	function index(){
		$this->initInfo();
		$nowPage	= session("bw_NowPage");
		$maxPage	= session("bw_MaxPage");
	
		if(isset($_GET['upPage'])&&$nowPage != 1)
			$nowPage -= 1;
		if(isset($_GET['downPage'])&&$nowPage != $maxPage)
			$nowPage += 1;
		if(isset($_GET['toPage']))
			$nowPage = $_GET['toPage'];
	
		session("bw_NowPage",$nowPage);
		
		$startItem = ($nowPage-1)*getUser("pagecount");
		$buglist	= M('buglist')->where('isdel = 0')->order("status,thedate desc")->limit($startItem,getUser("pagecount"))
		->getField('bugid,title,finduserid,ownerid,version,platform,level,module,status,thedate');
		
		$rBugList	= array();
		foreach($buglist as $bug){
			$indexNum	= stripos(C("BUG_LEVEL.".$bug['level']),":");
			$color	= substr(C("BUG_LEVEL.".$bug['level']),$indexNum+1);
			$bug['title']	= "<".$color.">".$bug['title']."</".$color.">";
			$bug['level']	= substr(C("BUG_LEVEL.".$bug['level']),0,$indexNum);
			$bug['status']	= C("BUG_STATUS.".$bug['status']);
			array_push($rBugList,$bug);
		}
			
			$this->assign("isMyBug",0);
			$this->assign("rBugList", $rBugList);
			$this->display('buglist');
			/*
		if($maxPage > 1){
			$pageHtml = "<li onclick=\"toPage('up')\">上一页</li>";
			if($nowPage<=5||$maxPage<=10){
				for($i=1; $i<=$maxPage; $i++){
					if($i == $nowPage)	$pageHtml	= "<li class='nowPage'>".$i."</li>".$pageHtml;
					else $pageHtml	= "<a href='getBuglist?toPage=".$i."'><li>".$i."</li></a>".$pageHtml;
				}
			}else{
				for($i=$nowPage-5;$i<$nowPage+5;$i++){
					if($i == $nowPage)	$pageHtml	= "<li class='nowPage'>".$i."</li>".$pageHtml;
					else $pageHtml	= "<a href='getBuglist?toPage=".$i."'><li>".$i."</li></a>".$pageHtml;
				}
			}
			$pageHtml	= "<li onClick=toPage('down')>下一页</li>".$pageHtml;
		}
	
		$this->assign("pageHtml",$pageHtml);
		$this->display("iframe");
		*/
	}
	
	/*添加Bug*/
	function addBug(){
		if($_POST){
			$bugData	= array(
					'title'		=> $_POST['title'],
					'content'	=> $_POST['content'],
					'finduserid'	=>	getUser('userid'),
					'ownerid'	=> 0,
					'module'	=> $_POST['module'],
					'status'	=> 1,
					'thedate'	=> strtotime(date("Y-m-d H:i:s")),
					'isdel'		=> 0,
					'level'		=> $_POST['level'],
					'platform'	=> $_POST['platform'],
					'version'	=> $_POST['version'],
			);
	
			$status = M('buglist')->data($bugData)->add();
			if($status){
				$logData	= array(
						"thedate"	=> strtotime(date("Y-m-d H:i:s")),
						"userid"	=> getUser("userid"),
						"opstype"	=> "addBug",
						"detail"	=> $status
				);
				M('opslog')->data($logData)->add();
				$this->redirect("/Home/Bug/index");
			}else{
				$this->error("添加失败，请重试!");
			}
		}else{
			$this->display('bugadd');
		}
	}
	
	/*搜索Bug*/
	function selectBug(){
		if($_POST){
			$db	= "isdel=0 and ";
			if($_POST['bug_title'] != null)
				$db .= "title like \"%$_POST[bug_title]%\" and ";
			if($_POST['ownerid'] != 0)
				$db	.= "ownerid = $_POST[ownerid] and ";
			if($_POST['module'] != 0)
				$db	.= "module = $_POST[module] and ";
			if($_POST['level'] != 0)
				$db	.= "level = $_POST[level] and ";
			if($_POST['status'] != 0)
				$db	.= "status = $_POST[status] and ";
			if($_POST['version'] != NULL)
				$db .= "version = '$_POST[version]' and ";
			if($_POST['platform'] != 0)
				$db	.= "platform = $_POST[platform] and ";
			if($_POST['startDate'] != NULL){
				$thedate = strtotime($_POST['startDate']);
				$db .= "thedate > $thedate and ";
			}
			if($_POST['endDate'] != Null){
				$thedate = strtotime($_POST['endDate']);
				$db	.= "thedate < $thedate and ";
			}
				
			$db	= substr($db,0,strlen($db)-4);
			$buglist	= M('buglist')->where($db)->order("bugid desc")->getField('bugid,title,finduserid,ownerid,version,platform,level,module,status,thedate');
			
			$logData	= array(
					"thedate"	=> strtotime(date("Y-m-d H:i:s")),
					"userid"	=> getUser("userid"),
					"opstype"	=> "selectBug",
					"detail"	=> $db,	
			);
			M("opslog")->data($logData)->add();
			
			$this->displayBugList($buglist,"lia_ss");
		}else{
			$this->assign("activeLi", "lia_ss");
			$this->display('selectBug1');
		}
	}
	
	/*Bug详细*/
	function detailBug($bugid = null){
		if($bugid || !$_POST){
			if($bugid == null)	$bugid	= (int)I("get.bugid");
			if($bugid == null) return 0;
			$detailBug	= M('buglist')->where('bugid = '.(int)$bugid)->getField('bugid,title,content,ownerid,version,platform,module,status,level');
			$detailBug	= $detailBug[$bugid];
		
			foreach($detailBug as $key=>$value){
				$this->assign("$key", $value);
			}
			$this->display("bugdetail");
		}else{
			dump($_POST);
		}
	}
	
	function changeBug(){
		if($_POST){
			$bugData	= $_POST;
			$bugData['thedate']	= strtotime(date("Y-m-d H:i:s"));
			$bugData['userid']	= getUser("userid");
			$bugid	= $_POST['bugid'];
			$status	= M("buglist")->where("bugid = ".(int)$bugid)->data($bugData)->save($bugData);
			if($status){
				$this->detailBug($bugid);
			}
			else 
				$this->error("变更失败,请重试!");
		}else{
			$this->error("变更失败,请重试!");
		}
	}
	
	
	
	/*自己的Bug*/
	function myBug(){
		$buglist	= M('buglist')->where("ownerid = ".(int)getUser("userid")." and status = 3")->order("status,thedate desc")->limit($startItem,C("BUG_PAGECOUNT"))
		->getField('bugid,title,finduserid,version,ownerid,level,module,status,thedate');
		
		$rBugList	= array();
		foreach($buglist as $bug){
			$indexNum	= stripos(C("BUG_LEVEL.".$bug['level']),":");
			$color	= substr(C("BUG_LEVEL.".$bug['level']),$indexNum+1);
			$bug['title']	= "<".$color.">".$bug['title']."</".$color.">";
			$bug['level']	= substr(C("BUG_LEVEL.".$bug['level']),0,$indexNum);
			$bug['status']	= C("BUG_STATUS.".$bug['status']);
			array_push($rBugList,$bug);
		}
			
			$this->assign("isMyBug",1);
			$this->assign("rBugList", $rBugList);
			$this->display('buglist');
	}
	
	/*加载必要信息*/
	function initInfo(){
		$bugItems	= M("buglist")->where("isdel = 0")->count();
		$maxPage	= ceil($bugItems/getUser('pagecount'));
		$allUser	= M('users')->getField("userid, nickname");
		$adminUser	= M('auth_group_access')->where("uid = ".getUser('userid')." and group_id = 3")->find();
		if($adminUser)
			session("bw_AdminUser",1);
		else 
			session("bw_AdminUser",0);
	
		$db = "(ownerid = ".(int)getUser("userid")." and status = 2) ";
		if(doAuth(null,"Home/Bug/confirmBug") == 10){
			$db .= "or status=1 ";
		}
		if(doAuth(null, "Home/Bug/verifyBug") == 10){
			$db .= "or status=3 ";
		}
	
		$ownerBug	= M("buglist")->where($db)->count();
		session("bw_OwnerBug", $ownerBug);
		session("bw_AllUser", $allUser);
		session("bw_NowPage", 1);
		session("bw_MaxPage", $maxPage);
	}
}